<?php
class Model_User extends Zend_Db_Table {
	
	/**
	 * name of the table
	 *
	 * @var string
	 */
		
	protected $_name = 'hl_users';
	
	public function init() {}
	
	public function get($filters, $order = null) {
		$select = $this->select()
    					->setIntegrityCheck(false)
    					->from(array('u' => $this->_name));
    	
    	// join user's informations
    	$select->join(array('ud' => 'hl_users_datas'), 'u.id = ud.id_user');
		
    	foreach($filters AS $key => $value) {
    		
    		/** TODO
    			If "extra" field
    		*/
    		
    		// If is array, IN()
    		if(is_array($value)) {
    			$temp = '';
    			foreach($value AS $val) {
    				$temp .= '"' . $val . '",';
    			}
    			$temp = trim($temp, ',');
    			
    			$select->where('LOWER(u.' . $key . ') IN ('.$temp.')');
    		
    		// Else	
    		} else {
				$select->where('LOWER(u.' . $key . ') = ?', $value);
			}
    	}
			
		if(!is_null($order)) {
			$select->order($order);
		}
		$result_temp = $this->fetchAll($select);
		
		$result = Model_Tools::stripslashes($result_temp);
		
		// get users' groups
		$ugM = new Model_UserGroup();
		foreach($result as &$user) {
			$filter = array('id_user' => $user['id']);
			$order = 'keyname';
			$groups = $ugM->get($filter);
		
			// if no group, not logged
			if(empty($groups)) {
				$user['groups'] = array();
			}
		
			foreach($groups as $g) {
				$user['groups'][] = $g['keyname'];
			}
		}
		
    	return $result;
	}
	
	public function add($datas) {
		
		// split datas
		$user = array(	'email' => $datas['email'],
						'nickname' => $datas['nickname'],
						'password' => '');
		
		foreach($user as &$value) {
			$value = addslashes($value);
		}
		$id = $this->insert($user_tab);
    	
    	// add user's datas
    	$udM = new Model_UsersDatas();
    	$id_datas = $udM->insert();
		
	}
	
	public function set($id, $datas) {
		
		// addslashes
		foreach($datas as &$value) {
			$value = addslashes($value);
		}
		
    	$result = $this->update($datas, array('id = ?' => $id));
    	return $result;
		
	}
    
    public function updateInformations($id) {
    	
    	// Get user's informations
    	$filter = array('id' => $id);
    	$user_temp = Model_User::get($filter);
		
		if(is_null($user_temp[0]) or empty($user_temp[0]) or count($user_temp) == 0) {
			return false;
		}
		
		$auth = Zend_Auth::getInstance();
		
    	// Fluch user's view informations <=> logout
		$auth = Zend_Auth::getInstance();
        $user = Zend_Auth::getInstance()->getIdentity();
        $auth->getStorage()->clear();
    	
    	// Save user's view informations <=> login
		$user['email'] = $user_temp[0]['email'];
		$user['password'] = $user_temp[0]['password'];
		$user['creation'] = $user_temp[0]['creation'];
		$user['update'] = $user_temp[0]['update'];
		$user['modifyer_id'] = $user_temp[0]['modifyer_id'];
		$user['status'] = $user_temp[0]['status'];
		$user['id_user'] = $user_temp[0]['id_user'];
		$user['firstname'] = $user_temp[0]['firstname'];
		$user['lastname'] = $user_temp[0]['lastname'];
		$user['keyfirstname'] = $user_temp[0]['keyfirstname'];
		$user['nickname'] = $user_temp[0]['nickname'];
		$user['keyname'] = $user_temp[0]['keyname'];
		$user['address'] = $user_temp[0]['address'];
		$user['postcode'] = $user_temp[0]['postcode'];
		$user['city'] = $user_temp[0]['city'];
		$user['telephone'] = $user_temp[0]['telephone'];
		$user['style'] = $user_temp[0]['style'];
		$user['song'] = $user_temp[0]['song'];
		$user['situation'] = $user_temp[0]['situation'];
		$user['url'] = $user_temp[0]['url'];
		$user['about'] = $user_temp[0]['about'];
		$user['facebook'] = $user_temp[0]['facebook'];
		$user['twitter'] = $user_temp[0]['twitter'];
		$user['youtube'] = $user_temp[0]['youtube'];
		$user['dailymotion'] = $user_temp[0]['dailymotion'];
		$user['birth'] = $user_temp[0]['birth'];
		$user['picture'] = $user_temp[0]['picture'];
		$user['modifyer_ip'] = $user_temp[0]['modifyer_ip'];
		
		// stock in session
		$auth = Zend_Auth::getInstance();
		$auth->getStorage()->write($user);
    	
    	return true;
    }
	
}
?>
